import matplotlib.pyplot as plt
import pickle

def xywh2xyxy(box):
    x1 = box[0] - box[2] / 2
    y1 = box[1] - box[3] / 2
    x2 = box[0] + box[2] / 2
    y2 = box[1] + box[2] / 2
    return [x1,y1,x2,y2]

def box2point(box):
    x1,y1,x2,y2 = box[0],box[1],box[2],box[3]
    point1= [x1,y1]
    point2 = [x2, y1]
    point3 = [x1, y2]
    point4 = [x2, y2]
    return point1,point2,point3,point4

if __name__ == "__main__":
    import numpy as np
    import cv2
    import random
    f1 = open('D:\python\Aclass\\1\myselfV1\\temp\l.pkl', 'rb')
    l = pickle.load(f1)
    f1.close()
    f2 = open('D:\python\Aclass\\1\myselfV1\\temp\gt_box.pkl', 'rb')
    gt_box = pickle.load(f2)
    f2.close()
    f3 = open('D:\python\Aclass\\1\myselfV1\\temp\pred_boxes.pkl', 'rb')
    pred_boxes = pickle.load(f3)
    f3.close()

    predict = pred_boxes.detach().numpy()
    for i in range(pred_boxes.detach().numpy().shape[0]):
        for q in l:
            anchor_box = predict[i][q[0]][q[1]][q[2]]
            x1 = anchor_box[0] - anchor_box[2] / 2
            y2 = anchor_box[1] - anchor_box[3] / 2
            plt.gca().add_patch(plt.Rectangle(xy=(x1, y2), width=anchor_box[2], height=anchor_box[3], fill=False, color='g'))
        for gt in gt_box:
            x1 = gt[0] - gt[2] / 2
            y2 = gt[1] - gt[3] / 2
            plt.gca().add_patch(plt.Rectangle(xy=(x1, y2), width=gt[2], height=gt[3], fill=False, color='r'))
        plt.xlim(0, 26)
        plt.ylim(0, 26)
        plt.grid()
        plt.show()
    # x1 = gt[0]-gt[2]/2
    # y2 = gt[1]-gt[3]/2
    # plt.gca().add_patch(plt.Rectangle(xy=(x1,y2),width=gt[2],height=gt[3],fill=False,color='r'))
    # for i in sets:
    #     x1 = i[0]-i[2]/2
    #     y2 = i[1] - i[3] / 2
    #     plt.gca().add_patch(plt.Rectangle(xy=(x1, y2), width=gt[2], height=gt[3], fill=False,color='g'))

